package com.example.administrator.dagger2demo.ui.work;

import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.dagger2demo.R;
import com.example.administrator.dagger2demo.adapter.inter.Indo;
import com.example.administrator.dagger2demo.adapter.inter.WorkAdapter;
import com.example.administrator.dagger2demo.application.DemoApplication;
import com.example.administrator.dagger2demo.ui.Base2Activity;
import com.example.administrator.dagger2demo.widget.header.RentalsSunHeaderView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.util.PtrLocalDisplay;

/**
 * Created by Administrator on 2016/4/20.
 */
public class WorkActivity extends Base2Activity implements WorkContract.View {
    WorkContract.WorkActionsListener workActionsListener;
    RecyclerView workRecyclerView;
    WorkAdapter workAdapter;

    ArrayList<Indo> mIndos = new ArrayList<>();
    @Bind(R.id.index_tv_title)
    TextView indexTvTitle;
    @Bind(R.id.index_iv_right)
    ImageView indexIvRight;
    @Bind(R.id.id_toolbar)
    Toolbar idToolbar;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.rotate_header_list_view_frame)
    PtrFrameLayout mPtrFrameLayout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        workActionsListener = DaggerWorkContractComponent
                .builder()
                .appComponent(DemoApplication.getAppComponent())
                .workPresenterModule(new WorkPresenterModule(this))
                .build().getWorkPresenter();


        //header
//        StoreHouseHeader header = new StoreHouseHeader(this);
//        header.setPadding(0, PtrLocalDisplay.dp2px(20), 0, PtrLocalDisplay.dp2px(20));
//        header.initWithString("Ultra PTR");

        // header
//        final StoreHouseHeader header = new StoreHouseHeader(this);
//        header.setPadding(0, PtrLocalDisplay.dp2px(15), 0, 0);
//        header.initWithPointList(getPointList());

        final RentalsSunHeaderView header = new RentalsSunHeaderView(this);
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        header.setPadding(0, PtrLocalDisplay.dp2px(15), 0, PtrLocalDisplay.dp2px(10));
        header.setUp(mPtrFrameLayout);

        //header
//        final MaterialHeader header = new MaterialHeader(this);
//        int[] headerColors = getResources().getIntArray(R.array.google_colors);
//        header.setColorSchemeColors(headerColors);
//        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
//        header.setPadding(0, PtrLocalDisplay.dp2px(15), 0, PtrLocalDisplay.dp2px(10));
//        header.setPtrFrameLayout(mPtrFrameLayout);



        mPtrFrameLayout.setHeaderView(header);
        mPtrFrameLayout.addPtrUIHandler(header);


        //footer
//        StoreHouseHeader footer = new StoreHouseHeader(this);
//        footer.setPadding(0, PtrLocalDisplay.dp2px(20), 0, PtrLocalDisplay.dp2px(20));
//        footer.initWithString("Ultra Footer");

        // footer
//        final StoreHouseHeader footer = new StoreHouseHeader(this);
//        footer.setPadding(0, PtrLocalDisplay.dp2px(15), 0, 0);
//        footer.initWithPointList(getPointList());

        // footer
        final RentalsSunHeaderView footer = new RentalsSunHeaderView(this);
        footer.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        footer.setPadding(0, PtrLocalDisplay.dp2px(15), 0, PtrLocalDisplay.dp2px(10));
        footer.setUp(mPtrFrameLayout);

        // footer
//        final MaterialHeader footer = new MaterialHeader(this);
//        int[] footerColors = getResources().getIntArray(R.array.google_colors);
//        footer.setColorSchemeColors(footerColors);
//        footer.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
//        footer.setPadding(0, PtrLocalDisplay.dp2px(15), 0, PtrLocalDisplay.dp2px(10));
//        footer.setPtrFrameLayout(mPtrFrameLayout);



        mPtrFrameLayout.setFooterView(footer);
        mPtrFrameLayout.addPtrUIHandler(footer);

//        mPtrFrameLayout.setPinContent(true);//原生状态
        mPtrFrameLayout.setLoadingMinTime(1000);
        mPtrFrameLayout.setDurationToCloseHeader(1500);
        mPtrFrameLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout ptrFrameLayout) {
                mPtrFrameLayout.refreshComplete();
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                mPtrFrameLayout.refreshComplete();
            }
        });


        Log.i("TAG", workActionsListener == null ? "workActionsListener==null" : "workActionsListener!=null");

    }

    private ArrayList<float[]> getPointList() {
        // this point is taken from https://github.com/cloay/CRefreshLayout
        List<Point> startPoints = new ArrayList<Point>();
        startPoints.add(new Point(240, 80));
        startPoints.add(new Point(270, 80));
        startPoints.add(new Point(265, 103));
        startPoints.add(new Point(255, 65));
        startPoints.add(new Point(275, 80));
        startPoints.add(new Point(275, 80));
        startPoints.add(new Point(302, 80));
        startPoints.add(new Point(275, 107));

        startPoints.add(new Point(320, 70));
        startPoints.add(new Point(313, 80));
        startPoints.add(new Point(330, 63));
        startPoints.add(new Point(315, 87));
        startPoints.add(new Point(330, 80));
        startPoints.add(new Point(315, 100));
        startPoints.add(new Point(330, 90));
        startPoints.add(new Point(315, 110));
        startPoints.add(new Point(345, 65));
        startPoints.add(new Point(357, 67));
        startPoints.add(new Point(363, 103));

        startPoints.add(new Point(375, 80));
        startPoints.add(new Point(375, 80));
        startPoints.add(new Point(425, 80));
        startPoints.add(new Point(380, 95));
        startPoints.add(new Point(400, 63));

        List<Point> endPoints = new ArrayList<Point>();
        endPoints.add(new Point(270, 80));
        endPoints.add(new Point(270, 110));
        endPoints.add(new Point(270, 110));
        endPoints.add(new Point(250, 110));
        endPoints.add(new Point(275, 107));
        endPoints.add(new Point(302, 80));
        endPoints.add(new Point(302, 107));
        endPoints.add(new Point(302, 107));

        endPoints.add(new Point(340, 70));
        endPoints.add(new Point(360, 80));
        endPoints.add(new Point(330, 80));
        endPoints.add(new Point(340, 87));
        endPoints.add(new Point(315, 100));
        endPoints.add(new Point(345, 98));
        endPoints.add(new Point(330, 120));
        endPoints.add(new Point(345, 108));
        endPoints.add(new Point(360, 120));
        endPoints.add(new Point(363, 75));
        endPoints.add(new Point(345, 117));

        endPoints.add(new Point(380, 95));
        endPoints.add(new Point(425, 80));
        endPoints.add(new Point(420, 95));
        endPoints.add(new Point(420, 95));
        endPoints.add(new Point(400, 120));
        ArrayList<float[]> list = new ArrayList<float[]>();

        int offsetX = Integer.MAX_VALUE;
        int offsetY = Integer.MAX_VALUE;

        for (int i = 0; i < startPoints.size(); i++) {
            offsetX = Math.min(startPoints.get(i).x, offsetX);
            offsetY = Math.min(startPoints.get(i).y, offsetY);
        }
        for (int i = 0; i < endPoints.size(); i++) {
            float[] point = new float[4];
            point[0] = startPoints.get(i).x - offsetX;
            point[1] = startPoints.get(i).y - offsetY;
            point[2] = endPoints.get(i).x - offsetX;
            point[3] = endPoints.get(i).y - offsetY;
            list.add(point);
        }
        return list;
    }

    @Override
    public int getContentView() {
        return R.layout.work;
    }

    @Override
    public boolean isShowLeftIcon() {
        return false;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_launcher;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.app_name;
    }

    public void ok(View view) {
        workActionsListener.login("1", "1");
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void closeProgress() {

    }

    @Override
    public void startShowView() {

    }
}
